{--
    This package provides compatible definitions for Haskell 2010's
    System.Exit by delegating to Java's System.exit function.

    Note that we do not raise an error for attempting to exitWith an
    ExitFailure 0 value (like GHC's System.Exit library does). That
    seems a rather arbitrary / unnecessary restriction although we
    could throw an IllegalArgumentException for it (which still would
    not be entirely compatible with Haskell anyway).
--}
module frege.system.Exit where

import frege.java.Lang

--- we rely default Eq, Ord, Show implementations - there is no Read yet
data ExitCode = ExitSuccess | ExitFailure Int
derive Eq ExitCode
derive Ord ExitCode
derive Show ExitCode

exitWith :: ExitCode -> IO a
exitWith ExitSuccess     = System.exit 0 >> return undefined
exitWith (ExitFailure n) = System.exit n >> return undefined

--- exitFail is arbitrarily 1 to indicate a failing exit
exitFailure :: IO a
exitFailure = exitWith (ExitFailure 1)

exitSuccess :: IO a
exitSuccess = exitWith ExitSuccess
